package day03;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/3/13 13:32
 * Offer 14- I. 剪绳子
 */
public class Offer14 {
    public int cuttingRope(int n) {
        if (n <= 2) {
            return 1;
        }
        if (n == 3) {
            return 2;
        }

        int res = n / 3;
        int mod = n % 3;

        if (mod == 0) {
            return pow(3, res);
        } else if (mod == 1) {
            return pow(3, res - 1) * 4;
        } else {
            return pow(3, res) * mod;
        }
    }

    int pow(int num, int res) {
        for (int i = 0; i < res; i++) {
            num *= num;
        }
        return num;
    }
}
